/*
 * vfileOpsSDL_RW.h
 *
 *  Created on: Jul 25, 2009
 *      Author: kurt
 */

#ifndef VFILEOPSSDL_RW_H_
#define VFILEOPSSDL_RW_H_

#include "VirtualFile.h"
#include <SDL/SDL_rwops.h>

bool vSDL_RWopen(const char * rwstruct, const char * mode, void *& stream)
{
	stream = (void *)rwstruct;
	return stream != NULL;
}
void vSDL_RWclose(void *& stream)
{
	if(stream == NULL) return;
	
	SDL_RWops * ops = (SDL_RWops *)stream;
	SDL_RWclose(ops);
	SDL_FreeRW(ops);
	stream = NULL;
}
int vSDL_RWseek(void * stream, long offset, int origin)
{
	if(stream == NULL) return 0;
	
	return SDL_RWseek( ( (SDL_RWops *)stream), offset, origin);
}
bool vSDL_eof(void * stream)
{
	if(stream == NULL) return true;
	
	long cur = SDL_RWtell( ( (SDL_RWops *)stream));
	long end = SDL_RWseek( ( (SDL_RWops *)stream), 0, SEEK_END);
	if(cur == end)
		return true;
	SDL_RWseek( ( SDL_RWops *)stream, cur, SEEK_SET);
	return false;
}
long vSDL_RWtell(void * stream)
{
	if(stream == NULL) return 0;
	
	return SDL_RWtell( ( (SDL_RWops *)stream));
}
int vSDL_RWread(void * buffer, size_t size, size_t num, void * stream)
{
	if(stream == NULL) return 0;
	
	return SDL_RWread( ( (SDL_RWops *)stream), buffer, size, num);
}
int vSDL_RWwrite(const void * buffer, size_t size, size_t count, void * stream)
{
	if(stream == NULL) return 0;
	
	return SDL_RWwrite( ( (SDL_RWops *)stream), buffer, size, count);
}
bool vSDL_RWis_open(void * stream)
{
	return stream != NULL;
}

#endif /* VFILEOPSSDL_RW_H_ */
